package com.oa.pm.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.pm.dto.UserQuestionnaireAddDTO;
import com.oa.pm.entity.UserQuestionnaire;
import com.oa.pm.entity.UserQuestionnaireAnswer;
import com.oa.pm.mapper.UserQuestionnaireMapper;
import com.oa.pm.service.IUserQuestionnaireAnswerService;
import com.oa.pm.service.IUserQuestionnaireService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.TopicDetailsVO;
import com.oa.pm.vo.UserQuestionnaireListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户填写问卷表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class UserQuestionnaireServiceImpl extends ServiceImpl<UserQuestionnaireMapper, UserQuestionnaire> implements IUserQuestionnaireService {

    private final UserQuestionnaireMapper userQuestionnaireMapper;

    @Autowired
    public UserQuestionnaireServiceImpl(UserQuestionnaireMapper userQuestionnaireMapper) {
        this.userQuestionnaireMapper = userQuestionnaireMapper;
    }

    @Autowired
    private IUserQuestionnaireAnswerService userQuestionnaireAnswerService;

    @Override
    public PageResult<UserQuestionnaireListVO> findUserQuestionnaireList(Integer pageNum, Integer pageSize, String title, String id) {
        return new PageResult<>(userQuestionnaireMapper.findUserQuestionnaireList(new Page(pageNum, pageSize), title, id));
    }

    @Override
    public PageResult<UserQuestionnaireListVO> findMyUserQuestionnaireList(Integer pageNum, Integer pageSize, String title, String id) {
        return new PageResult<>(userQuestionnaireMapper.findMyUserQuestionnaireList(new Page(pageNum, pageSize), title, id));
    }

    @Override
    public List<TopicDetailsVO> findTopicByQuestionnaireId(String id) {
        return userQuestionnaireMapper.findTopicByQuestionnaireId(id);
    }

    @Override
    public List<TopicDetailsVO> findAnswer(String id, String userId) {
        return userQuestionnaireMapper.findAnswer(id, userId);
    }

    @Override
    public boolean add(UserQuestionnaireAddDTO userQuestionnaireAddDTO) {
        UserQuestionnaire userQuestionnaire = new UserQuestionnaire();
        userQuestionnaire.setQuestionnaireId(userQuestionnaireAddDTO.getQuestionnaireId());
        userQuestionnaire.setUserId(userQuestionnaireAddDTO.getUserId());
        userQuestionnaire.setSubmitTime(new Date());
        if (insert(userQuestionnaire)) {
            List<UserQuestionnaireAnswer> userQuestionnaireAnswers = new ArrayList<>();
            userQuestionnaireAddDTO.getUserQuestionnaireAnswers().forEach(d -> {
                UserQuestionnaireAnswer userQuestionnaireAnswer = new UserQuestionnaireAnswer();
                BeanUtils.copyProperties(d, userQuestionnaireAnswer);
                userQuestionnaireAnswer.setUserQuestionnaireId(userQuestionnaire.getId());
                userQuestionnaireAnswers.add(userQuestionnaireAnswer);
            });
            if (userQuestionnaireAnswerService.insertBatch(userQuestionnaireAnswers)) {
                return true;
            }
        }
        return false;
    }
}
